package com.aurora.calculator.ui.theme

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.TweenSpec
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color


@Stable
class CalculatorThemeColor(
    BackgroundColor: Color,
    SymbolColor: Color,
    ButtonColor: Color,
    ButtonTextColor: Color,
    ResultColor: Color,
    AcColor: Color,
    AcTextColor: Color,
    EqualColor: Color,
    EqualTextColor: Color
) {
    var BackgroundColor: Color by mutableStateOf(BackgroundColor)
        private set
    var SymbolColor: Color by mutableStateOf(SymbolColor)
        private set
    var ButtonColor: Color by mutableStateOf(ButtonColor)
        private set
    var ButtonTextColor: Color by mutableStateOf(ButtonTextColor)
        private set
    var ResultColor: Color by mutableStateOf(ResultColor)
        private set
    var AcColor: Color by mutableStateOf(AcColor)
        private set
    var AcTextColor: Color by mutableStateOf(AcTextColor)
        private set
    var EqualColor: Color by mutableStateOf(EqualColor)
        private set
    var EqualTextColor: Color by mutableStateOf(EqualTextColor)
        private set
}


private val DarkColorScheme = CalculatorThemeColor(
    BackgroundColor = BackgroundColor40,
    SymbolColor = SymbolColor40,
    ButtonColor = ButtonColor40,
    ButtonTextColor = ButtonTextColor40,
    ResultColor = ResultColor40,
    AcColor = AcColor40,
    AcTextColor = AcTextColor40,
    EqualColor = EqualColor40,
    EqualTextColor = EqualTextColor40
)

private val LightColorScheme = CalculatorThemeColor(
    BackgroundColor = BackgroundColor80,
    SymbolColor = SymbolColor80,
    ButtonColor = ButtonColor80,
    ButtonTextColor = ButtonTextColor80,
    ResultColor = ResultColor80,
    AcColor = AcColor80,
    AcTextColor = AcTextColor80,
    EqualColor = EqualColor80,
    EqualTextColor = EqualTextColor80
)

@Composable
private fun CalculatorAnimatedColors(colorScheme: CalculatorThemeColor): CalculatorThemeColor {
    val BackgroundColor = animateColorAsState(colorScheme.BackgroundColor, TweenSpec(600))
    val SymbolColor = animateColorAsState(colorScheme.SymbolColor, TweenSpec(600))
    val ButtonColor = animateColorAsState(colorScheme.ButtonColor, TweenSpec(600))
    val ButtonTextColor = animateColorAsState(colorScheme.ButtonTextColor, TweenSpec(600))
    val ResultColor = animateColorAsState(colorScheme.ResultColor, TweenSpec(600))
    val AcColor = animateColorAsState(colorScheme.AcColor, TweenSpec(600))
    val AcTextColor = animateColorAsState(colorScheme.AcTextColor, TweenSpec(600))
    val EqualColor = animateColorAsState(colorScheme.EqualColor, TweenSpec(600))
    val EqualTextColor = animateColorAsState(colorScheme.EqualTextColor, TweenSpec(600))

    return CalculatorThemeColor(
        BackgroundColor = BackgroundColor.value,
        SymbolColor = SymbolColor.value,
        ButtonColor = ButtonColor.value,
        ButtonTextColor = ButtonTextColor.value,
        ResultColor = ResultColor.value,
        AcColor = AcColor.value,
        AcTextColor = AcTextColor.value,
        EqualColor = EqualColor.value,
        EqualTextColor = EqualTextColor.value
    )
}

private val LocalCalculatorColors = compositionLocalOf {
    LightColorScheme
}

object CalculatorTheme {
    val colors: CalculatorThemeColor
        @Composable
        get() = LocalCalculatorColors.current
}

@Composable
fun CalculatorTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = CalculatorAnimatedColors(if (darkTheme) DarkColorScheme else LightColorScheme)

    CompositionLocalProvider(LocalCalculatorColors provides colors) {
        MaterialTheme(
            content = content
        )
    }
}
